#!/usr/bin/env python
# -*- coding: utf-8 -*-

# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2022 - Wanderer Fan <wandererfan@gmail.com>             *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************
"""Migrates drawings made in FreeCAD v0.18 to v0.19."""

__Name__ = 'TechDrawMigrateFromv018'
__Comment__ = 'Convert v018 Rotation property to v019'
__Author__ = 'WandererFan'
__Version__ = '0.1.0'
__License__ = 'CC-BY-3.0'
__Web__ = 'http://www.freecadweb.org/'
__Wiki__ = 'http://www.freecadweb.org/wiki/Macro_TechDrawViewSet'
__Icon__ = ''
__Help__ = 'Open a v018 file in v019, execute'
__Status__ = 'Alpha'
__Requires__ = ''
__Communication__ = 'https://github.com/FreeCAD/FreeCAD-macros/issues/'
__Files__ = ''


# convert v0.18 Rotation values to v0.19

import FreeCAD as App
import TechDraw

def TechDrawMigrateFromv018():
    increment = App.Units.Quantity(270, App.Units.Unit("deg"))
    for obj in App.ActiveDocument.Objects:
        if obj.isDerivedFrom("TechDraw::DrawViewSection"):
            if obj.Rotation != 0.0:
                obj.Rotation = obj.Rotation + increment
                obj.touch()
            for inObj in obj.InList:
                if obj.isDerivedFrom("TechDraw::DrawViewDimension"):
                    inObj.touch()
        elif obj.isDerivedFrom("TechDraw::DrawViewPart"):
            if obj.Rotation != 0.0:
                obj.Rotation = -obj.Rotation
                obj.touch()


if __name__ == '__main__':
    TechDrawMigrateFromv018()
    App.ActiveDocument.recompute()

